<template>
  <div class="goodslist_container">
    <div class="goods_wrapper">
      <div class="item"
        v-for="item in goodslist" :key="item.id"
        @click="goGoodsInfo(item.id)">
        <img v-lazy="item.img_url">
        <h3 class="title">{{ item.title }}</h3>
        <div class="info">
          <p class="price">
            <span class="now">￥{{ item.sell_price }}</span>
            <span class="old">￥{{ item.market_price }}</span>
          </p>
          <p class="sell">
            <span>热卖中</span>
            <span class="old">剩{{ item.stock_quantity }}件</span>
          </p>
        </div>
      </div>
    </div>

    <mt-button type="danger" size="large" @click="getMore" :disabled="noMore">加载更多</mt-button>
  </div>
</template>

<script>
import { Toast } from 'mint-ui'

export default {
  name: 'goodslist',
  data() {
    return {
      pageNum: 1,
      goodslist: [],
      noMore: false
    }
  },
  methods: {
    getGoodsList() {
      this.$store.dispatch('getGoods', this.pageNum).then(res => {
        if(res.data.code === 200) {
          if(res.data.list == ''){
            Toast('没有更多内容了')
            this.noMore = true
          }
          
          res.data.list.forEach(item => {
            item.img_url = '/static/'+ item.img_url
          });
          this.goodslist = this.goodslist.concat(res.data.list)
        }
      })
    },
    getMore() {
      this.pageNum++
      this.getGoodsList()
    },
    goGoodsInfo(id) {
      this.$router.push({ name: 'goodsinfo', params: { id } })
    }
  },
  created() {
    this.getGoodsList()
  }
}
</script>

<style lang="less" scoped>
  .goods_wrapper {
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
    padding: 8px;
    .item {
      width: 49%;
      padding: 2px;
      margin: 3px 0;
      border: 1px solid #ccc;
      box-shadow: 0 0 8px #ccc;
      display: flex;
      flex-direction: column;
      img{
        width: 100%;
      }
      .title {
        max-height: 50px;
        font-size: 14px;
        padding: 5px 0;
        overflow: hidden;
        text-overflow: ellipsis;
        flex: 1;
      }
      .info {
        background-color: #eee;
        p {
          padding: 5px;
          margin: 0;
        }
        .price {
          .now {
            color: red;
            font-weight: bold;
            font-size: 16px;
          }
          .old {
            text-decoration: line-through;
            font-size: 12px;
            margin-left: 10px;
          }
        }
        .sell {
          font-size: 12px;
          display: flex;
          justify-content: space-between;
        }
      }
    }
  }
</style>
